<template>
  <div>
    <el-card style="margin: 20px 0">
      <!--全局组件-->
      <CategorySelect @getCategoryId="getCategoryId" :show="scene!=0"></CategorySelect>
    </el-card>
    <el-card>
      <!--底部为三部分组成-->
      <!--  SPU-->
      <div v-show="scene === 0">
        <el-button type="primary" icon="el-icon-plus" @click="addSpu" :disabled="!category3Id">添加SPU</el-button>
        <!--      表格：展示平台属性-->
        <el-table width="100%" border :data="records">
          <el-table-column type="index" label="序号" width="80" align="center">
          </el-table-column>
          <el-table-column prop="spuName" label="spu名称" width="width">
          </el-table-column>
          <el-table-column prop="description" label="spu描述" width="width">
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{row,$index}">
              <hint-button type="success" size="mini" icon="el-icon-plus" title="添加sku"
                @click="addSku(row)"></hint-button>
              <hint-button type="warning" size="mini" icon="el-icon-edit" title="修改spu"
                @click="updateSpu(row)"></hint-button>
              <hint-button type="info" size="mini" icon="el-icon-info" title="查看当前spu的全部sku"
                @click="handler(row)"></hint-button>
              <el-popconfirm title="这是一段内容确定删除嘛？" @onConfirm="deleteSpu(row)">
                <hint-button type="danger" size="mini" icon="el-icon-delete" title="删除spu"
                  slot="reference"></hint-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!--分页器-->
        <div class="block">
          <el-pagination style="text-align: center" :current-page="page" :page-sizes="[3, 5, 10]" :page-size="limit"
            layout="prev, pager, next, jumper,->, sizes,total" :total="total" @current-change="handleCurrentChange"
            @size-change="handleSizeChange">
          </el-pagination>
        </div>
      </div>
      <SpuForm v-show="scene === 1" @changeScene="changeScene" ref="spu">添加spu|修改spu</SpuForm>
      <SkuForm v-show="scene === 2" ref="sku" @changeScenes="changeScenes">添加sku</SkuForm>
    </el-card>
    <el-dialog :title="`${spu.spuName}的sku列表`" :visible.sync="dialogTableVisible" :before-close="close">
      <!-- table展示sku列表 -->
      <el-table :data="skuList" style="width:100%" border v-loading="loading">
        c
      </el-table>
    </el-dialog>
  </div>

</template>

<script>
import SkuForm from '@/views/product/Spu/SkuForm'
import SpuForm from '@/views/product/Spu/SpuForm'
export default {
  name: 'Spu',
  data() {
    return {
      //分类的id
      category1Id: '',
      category2Id: '',
      category3Id: '',
      page: 1,//当前为第几页
      limit: 3, //显示数据条数
      records: [],//spu列表的数据
      total: 0,//分页器一共需要展示数据的条数
      scene: 0,//0代表展示spu列表数据   1代表添加|修改spu  2代表 添加sku
      //控制对话框的显示和隐藏
      dialogTableVisible: false,
      spu: {},
      //存储sku列表的数据
      skuList: [],
      loading: true,
    }
  },
  components: {
    SkuForm, SpuForm
  },
  methods: {
    //自定义事件的回调
    getCategoryId({ categoryId, level }) {
      if (level === 1) {
        //区别三级分类的id，以及父组件进行存储
        this.category1Id = categoryId
        this.category2Id = ''
        this.category3Id = ''
      } else if (level === 2) {
        this.category2Id = categoryId
        this.category3Id = ''
      } else {
        //代表三级分类的id有了
        this.category3Id = categoryId
        //发请求获取品牌属性
        this.getSpuList()
      }
    },
    //获取SPU列表数据的方法
    async getSpuList() {
      //解构出数据
      const { page, limit, category3Id } = this
      //携带三个数据：page第几页，limit：展示几条数据，三级列表的id
      let result = await this.$API.spu.reqSpuList(page, limit, category3Id)
      if (result.code === 200) {
        this.total = result.data.total;
        this.records = result.data.records;
      }
    },
    //点击页码展示数据
    handleCurrentChange(page) {
      this.page = page;
      this.getSpuList();
    },
    //当每页显示数量改变时触发
    handleSizeChange(limit) {
      //修改参数
      this.limit = limit;
      //发请求
      this.getSpuList()
    },
    //添加spu按钮的回调
    addSpu() {
      //切换场景为1
      this.scene = 1
      //通知子组件spuForm发请求
      this.$refs.spu.addSpuData(this.category3Id)
    },
    //修改spu按钮的回调
    updateSpu(row) {
      this.scene = 1
      //获取子组件的方法
      //在父组件中可以通过$ref获取子组件等等
      this.$refs.spu.initSpuData(row);
    },
    //点击回到scene=0处
    changeScene({ scene, flag }) {
      //切换场景
      this.scene = scene;
      //子组件通知父组件切换场景，需要再次获取spu列表的数据进行展示
      if (flag == '修改') {
        this.getSpuList(this.page);
      } else {
        this.getSpuList()
      }
    },
    //删除spu的按钮的回调
    async deleteSpu(row) {
      //发请求
      let result = await this.$API.spu.reqDeleteSpu(row.id);
      if (result.code == 200) {
        this.$message({ type: 'success', message: '删除成功' });
      }
      this.getSpuList(this.records.length > 1 ? this.page : this.page - 1);
    },
    //添加SKU
    addSku(row) {
      //切换场景
      this.scene = 2;
      //父组件调用子组件
      this.$refs.sku.getData(this.category1Id, this.category2Id, row);
    },
    //skuForm通知父组件修改场景
    changeScenes() {
      this.scene = 0;
    },
    //查看sku按钮的回调
    async handler(spu) {
      //点击按钮的时候，对话框是可见的
      this.dialogTableVisible = true;
      //保存spu信息
      this.spu = spu;
      //获取sku列表的数据进行展示
      let result = await this.$API.sku.reqSkuList(spu.id);
      if (result.code == 200) {
        this.skuList = result.data
        //loading加载停止
        this.loading = false;
      }
    },
    //关闭对话框
    close(done) {
      //loading变回真
      this.loading = true;
      //清空列表
      this.skuList = [];
      //关闭对话框
      done();
    }
  }
}
</script>

<style>

</style>
